{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n# Tight Layout guide\n\n\nHow to use tight-layout to fit plots within your figure cleanly.\n\n*tight_layout* automatically adjusts subplot params so that the\nsubplot(s) fits in to the figure area. This is an experimental\nfeature and may not work for some cases. It only checks the extents\nof ticklabels, axis labels, and titles.\n\nAn alternative to *tight_layout* is :doc:`constrained_layout\n</tutorials/intermediate/constrainedlayout_guide>`.\n\n\nSimple Example\n==============\n\nIn matplotlib, the location of axes (including subplots) are specified in\nnormalized figure coordinates. It can happen that your axis labels or\ntitles (or sometimes even ticklabels) go outside the figure area, and are thus\nclipped.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# sphinx_gallery_thumbnail_number = 7\n\nimport matplotlib.pyplot as plt\nimport numpy as np\n\nplt.rcParams['savefig.facecolor'] = \"0.8\"\n\n\ndef example_plot(ax, fontsize=12):\n    ax.plot([1, 2])\n\n    ax.locator_params(nbins=3)\n    ax.set_xlabel('x-label', fontsize=fontsize)\n    ax.set_ylabel('y-label', fontsize=fontsize)\n    ax.set_title('Title', fontsize=fontsize)\n\nplt.close('all')\nfig, ax = plt.subplots()\nexample_plot(ax, fontsize=24)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To prevent this, the location of axes needs to be adjusted. For\nsubplots, this can be done by adjusting the subplot params\n(`howto-subplots-adjust`). Matplotlib v1.1 introduces a new\ncommand :func:`~matplotlib.pyplot.tight_layout` that does this\nautomatically for you.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots()\nexample_plot(ax, fontsize=24)\nplt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that :func:`matplotlib.pyplot.tight_layout` will only adjust the\nsubplot params when it is called.  In order to perform this adjustment each\ntime the figure is redrawn, you can call ``fig.set_tight_layout(True)``, or,\nequivalently, set the ``figure.autolayout`` rcParam to ``True``.\n\nWhen you have multiple subplots, often you see labels of different\naxes overlapping each other.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.close('all')\n\nfig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nexample_plot(ax4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ":func:`~matplotlib.pyplot.tight_layout` will also adjust spacing between\nsubplots to minimize the overlaps.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nexample_plot(ax4)\nplt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ":func:`~matplotlib.pyplot.tight_layout` can take keyword arguments of\n*pad*, *w_pad* and *h_pad*. These control the extra padding around the\nfigure border and between subplots. The pads are specified in fraction\nof fontsize.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(nrows=2, ncols=2)\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nexample_plot(ax4)\nplt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    ":func:`~matplotlib.pyplot.tight_layout` will work even if the sizes of\nsubplots are different as far as their grid specification is\ncompatible. In the example below, *ax1* and *ax2* are subplots of a 2x2\ngrid, while *ax3* is of a 1x2 grid.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.close('all')\nfig = plt.figure()\n\nax1 = plt.subplot(221)\nax2 = plt.subplot(223)\nax3 = plt.subplot(122)\n\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\n\nplt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "It works with subplots created with\n:func:`~matplotlib.pyplot.subplot2grid`. In general, subplots created\nfrom the gridspec (:doc:`/tutorials/intermediate/gridspec`) will work.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.close('all')\nfig = plt.figure()\n\nax1 = plt.subplot2grid((3, 3), (0, 0))\nax2 = plt.subplot2grid((3, 3), (0, 1), colspan=2)\nax3 = plt.subplot2grid((3, 3), (1, 0), colspan=2, rowspan=2)\nax4 = plt.subplot2grid((3, 3), (1, 2), rowspan=2)\n\nexample_plot(ax1)\nexample_plot(ax2)\nexample_plot(ax3)\nexample_plot(ax4)\n\nplt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Although not thoroughly tested, it seems to work for subplots with\naspect != \"auto\" (e.g., axes with images).\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "arr = np.arange(100).reshape((10, 10))\n\nplt.close('all')\nfig = plt.figure(figsize=(5, 4))\n\nax = plt.subplot(111)\nim = ax.imshow(arr, interpolation=\"none\")\n\nplt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Caveats\n=======\n\n * :func:`~matplotlib.pyplot.tight_layout` only considers ticklabels, axis\n   labels, and titles. Thus, other artists may be clipped and also may\n   overlap.\n\n * It assumes that the extra space needed for ticklabels, axis labels,\n   and titles is independent of original location of axes. This is\n   often true, but there are rare cases where it is not.\n\n * pad=0 clips some of the texts by a few pixels. This may be a bug or\n   a limitation of the current algorithm and it is not clear why it\n   happens. Meanwhile, use of pad at least larger than 0.3 is\n   recommended.\n\nUse with GridSpec\n=================\n\nGridSpec has its own :func:`~matplotlib.gridspec.GridSpec.tight_layout` method\n(the pyplot api :func:`~matplotlib.pyplot.tight_layout` also works).\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import matplotlib.gridspec as gridspec\n\nplt.close('all')\nfig = plt.figure()\n\ngs1 = gridspec.GridSpec(2, 1)\nax1 = fig.add_subplot(gs1[0])\nax2 = fig.add_subplot(gs1[1])\n\nexample_plot(ax1)\nexample_plot(ax2)\n\ngs1.tight_layout(fig)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You may provide an optional *rect* parameter, which specifies the bounding box\nthat the subplots will be fit inside. The coordinates must be in normalized\nfigure coordinates and the default is (0, 0, 1, 1).\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig = plt.figure()\n\ngs1 = gridspec.GridSpec(2, 1)\nax1 = fig.add_subplot(gs1[0])\nax2 = fig.add_subplot(gs1[1])\n\nexample_plot(ax1)\nexample_plot(ax2)\n\ngs1.tight_layout(fig, rect=[0, 0, 0.5, 1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For example, this can be used for a figure with multiple gridspecs.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig = plt.figure()\n\ngs1 = gridspec.GridSpec(2, 1)\nax1 = fig.add_subplot(gs1[0])\nax2 = fig.add_subplot(gs1[1])\n\nexample_plot(ax1)\nexample_plot(ax2)\n\ngs1.tight_layout(fig, rect=[0, 0, 0.5, 1])\n\ngs2 = gridspec.GridSpec(3, 1)\n\nfor ss in gs2:\n    ax = fig.add_subplot(ss)\n    example_plot(ax)\n    ax.set_title(\"\")\n    ax.set_xlabel(\"\")\n\nax.set_xlabel(\"x-label\", fontsize=12)\n\ngs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.5)\n\n# We may try to match the top and bottom of two grids ::\ntop = min(gs1.top, gs2.top)\nbottom = max(gs1.bottom, gs2.bottom)\n\ngs1.update(top=top, bottom=bottom)\ngs2.update(top=top, bottom=bottom)\nplt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "While this should be mostly good enough, adjusting top and bottom\nmay require adjustment of hspace also.  To update hspace & vspace, we\ncall :func:`~matplotlib.gridspec.GridSpec.tight_layout` again with updated\nrect argument. Note that the rect argument specifies the area including the\nticklabels, etc.  Thus, we will increase the bottom (which is 0 for the normal\ncase) by the difference between the *bottom* from above and the bottom of each\ngridspec. Same thing for the top.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig = plt.gcf()\n\ngs1 = gridspec.GridSpec(2, 1)\nax1 = fig.add_subplot(gs1[0])\nax2 = fig.add_subplot(gs1[1])\n\nexample_plot(ax1)\nexample_plot(ax2)\n\ngs1.tight_layout(fig, rect=[0, 0, 0.5, 1])\n\ngs2 = gridspec.GridSpec(3, 1)\n\nfor ss in gs2:\n    ax = fig.add_subplot(ss)\n    example_plot(ax)\n    ax.set_title(\"\")\n    ax.set_xlabel(\"\")\n\nax.set_xlabel(\"x-label\", fontsize=12)\n\ngs2.tight_layout(fig, rect=[0.5, 0, 1, 1], h_pad=0.5)\n\ntop = min(gs1.top, gs2.top)\nbottom = max(gs1.bottom, gs2.bottom)\n\ngs1.update(top=top, bottom=bottom)\ngs2.update(top=top, bottom=bottom)\n\ntop = min(gs1.top, gs2.top)\nbottom = max(gs1.bottom, gs2.bottom)\n\ngs1.tight_layout(fig, rect=[None, 0 + (bottom-gs1.bottom),\n                            0.5, 1 - (gs1.top-top)])\ngs2.tight_layout(fig, rect=[0.5, 0 + (bottom-gs2.bottom),\n                            None, 1 - (gs2.top-top)],\n                 h_pad=0.5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Legends and Annotations\n=======================\n\nPre Matplotlib 2.2, legends and annotations were excluded from the bounding\nbox calculations that decide the layout.  Subsequently these artists were\nadded to the calculation, but sometimes it is undesirable to include them.\nFor instance in this case it might be good to have the axes shring a bit\nto make room for the legend:\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(4, 3))\nlines = ax.plot(range(10), label='A simple plot')\nax.legend(bbox_to_anchor=(0.7, 0.5), loc='center left',)\nfig.tight_layout()\nplt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "However, sometimes this is not desired (quite often when using\n``fig.savefig('outname.png', bbox_inches='tight')``).  In order to\nremove the legend from the bounding box calculation, we simply set its\nbounding ``leg.set_in_layout(False)`` and the legend will be ignored.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "fig, ax = plt.subplots(figsize=(4, 3))\nlines = ax.plot(range(10), label='B simple plot')\nleg = ax.legend(bbox_to_anchor=(0.7, 0.5), loc='center left',)\nleg.set_in_layout(False)\nfig.tight_layout()\nplt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Use with AxesGrid1\n==================\n\nWhile limited, the axes_grid1 toolkit is also supported.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from mpl_toolkits.axes_grid1 import Grid\n\nplt.close('all')\nfig = plt.figure()\ngrid = Grid(fig, rect=111, nrows_ncols=(2, 2),\n            axes_pad=0.25, label_mode='L',\n            )\n\nfor ax in grid:\n    example_plot(ax)\nax.title.set_visible(False)\n\nplt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Colorbar\n========\n\nIf you create a colorbar with the :func:`~matplotlib.pyplot.colorbar`\ncommand, the created colorbar is an instance of Axes, *not* Subplot, so\ntight_layout does not work. With Matplotlib v1.1, you may create a\ncolorbar as a subplot using the gridspec.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "plt.close('all')\narr = np.arange(100).reshape((10, 10))\nfig = plt.figure(figsize=(4, 4))\nim = plt.imshow(arr, interpolation=\"none\")\n\nplt.colorbar(im, use_gridspec=True)\n\nplt.tight_layout()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Another option is to use AxesGrid1 toolkit to\nexplicitly create an axes for colorbar.\n\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from mpl_toolkits.axes_grid1 import make_axes_locatable\n\nplt.close('all')\narr = np.arange(100).reshape((10, 10))\nfig = plt.figure(figsize=(4, 4))\nim = plt.imshow(arr, interpolation=\"none\")\n\ndivider = make_axes_locatable(plt.gca())\ncax = divider.append_axes(\"right\", \"5%\", pad=\"3%\")\nplt.colorbar(im, cax=cax)\n\nplt.tight_layout()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
